package zhcms.framework.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import zhcms.framework.GyReplace;
import zhcms.framework.GySplit;


public class GyGetFor {
	
	private String sContent;
	private String currnS;
	
	public GyGetFor()
	{
		sContent="";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void ParseFor(String gytFrontSeparator,String gytBackSeparator,HashMap<String, ArrayList<HashMap<String, String>>> gytInFor,String forKeyWord){
		//System.out.println("处理前中的内容:[[[[[[["+currnS);
		//System.out.println("处理中forKeyWord的内容:[[[[[[["+gytFrontSeparator+ forKeyWord + ".");
		
		Iterator<Entry<String, ArrayList<HashMap<String, String>>>> iSer = gytInFor.entrySet().iterator();
		while (iSer.hasNext()) {
			Map.Entry entry = (Map.Entry) iSer.next();
			
			
			if (forKeyWord.equals(entry.getKey().toString())) {
				ArrayList<HashMap<String, String>> gytempA = (ArrayList<HashMap<String, String>>) entry.getValue();
				// 得到块中一共有多少个变量
				GySplit e = new GySplit();
				e.tool(this.currnS, gytFrontSeparator+ forKeyWord + ".");
				String[] itemp_array = e.Show();
				e = null;
				
				//System.out.println("得到块中一共有多少个变量["+ itemp_array.length + "]");
				
				String[] gytiParameterArray;
				gytiParameterArray = new String[itemp_array.length - 1];
				
				if (itemp_array.length > 0) {

					// 得到标签
					for (int i = 1; i < itemp_array.length; i++) {
						GySplit n = new GySplit();
						n.tool(itemp_array[i], gytBackSeparator);
						String[] temp_end_array = n.Show();
						n = null;
						// String[]
						// temp_end_array=tempContent[i].split(this.gytBackSeparator);
						gytiParameterArray[i - 1] = temp_end_array[0];

					}
				}
				
				String tempSS = "";
				// 循环块的次数
				for (int j = 0; j < gytempA.size(); j++) {
					HashMap<String, String> map = (HashMap<String, String>) gytempA
							.get(j);
					String tempLSS = "";
					String tempLSA = this.currnS;
					for (int m = 0; m < gytiParameterArray.length; m++) {
						GyReplace r = new GyReplace();
						r.replaceAllNew(tempLSA, gytFrontSeparator+ forKeyWord + "."+ gytiParameterArray[m]+ gytBackSeparator,map.get(gytiParameterArray[m]));
						tempLSS = r.Show();
						tempLSA = r.Show();
						r = null;
					}
					tempSS = tempSS + tempLSS;
				}
				this.sContent =  tempSS ;
				//this.setsContent(tempSS);
				//System.out.println("处理后中的内容:[[[[[[["+tempSS);
			}
			
		}

	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void ParseForStep(String gytFrontSeparator,String gytBackSeparator,HashMap<String, ArrayList<HashMap<String, String>>> gytInFor,String forKeyWord,String StepNum){
		//System.out.println("处理前中的内容:[[[[[[["+currnS);
		//System.out.println("处理中forKeyWord的内容:[[[[[[["+gytFrontSeparator+ forKeyWord + ".");
		
		Iterator<Entry<String, ArrayList<HashMap<String, String>>>> iSer = gytInFor.entrySet().iterator();
		while (iSer.hasNext()) {
			Map.Entry entry = (Map.Entry) iSer.next();
			
			if (forKeyWord.equals(entry.getKey().toString())) {
				
				ArrayList<HashMap<String, String>> gytempA = (ArrayList<HashMap<String, String>>) entry.getValue();
				
				int stepn=Integer.parseInt(StepNum.trim());
				
				// 得到块中一共有多少个变量
				GySplit e = new GySplit();
				e.tool(this.currnS,  "].");
				String[] itemp_array = e.Show();
				e = null;
				
				//System.out.println("得到块中一共有多少个变量["+( itemp_array.length -1) + "]");
				
				String[] gytiParameterArray;
				gytiParameterArray = new String[itemp_array.length - 1];
				
				if (itemp_array.length > 0) {

					// 得到标签
					for (int i = 1; i < itemp_array.length; i++) {
						GySplit n = new GySplit();
						n.tool(itemp_array[i], gytBackSeparator);
						String[] temp_end_array = n.Show();
						n = null;
						// String[]
						// temp_end_array=tempContent[i].split(this.gytBackSeparator);
						gytiParameterArray[i - 1] = temp_end_array[0];
						//System.out.println("得变量["+ temp_end_array[0] + "]");
					}
				}
				
				String tempSS = "";
				// 循环块的次数
				for (int j = 0; j < gytempA.size(); j=j+stepn) {
					String tempLSS = "";
					String tempLSA = this.currnS;
					for(int mm=0;mm<stepn;mm++)
					{
						//dui 步长进行循环
						if((j+mm)>=gytempA.size())
						{
							for (int m = 0; m < gytiParameterArray.length; m++) {
								//查找字符串是否存在，如果存在，替换，不存在，跳过
								//
								String[] tempkeywordMain = null;
								GySplit bm = new GySplit();
								bm.tool(tempLSA, gytFrontSeparator+ forKeyWord+"["+mm+"]" + "."+ gytiParameterArray[m]+ gytBackSeparator);
								tempkeywordMain = bm.Show();
								bm = null;
								//
								if(tempkeywordMain.length>=2)
								{

									GyReplace r = new GyReplace();
									r.replaceAllNew(tempLSA, gytFrontSeparator+ forKeyWord+"["+mm+"]" + "."+ gytiParameterArray[m]+ gytBackSeparator,"");
									tempLSS = r.Show();
									tempLSA = r.Show();
									r = null;
								}
								else
								{
									
								}
							}
						}
						else
						{
							//System.out.println("(MM:"+mm+")(AAAA)[["+ tempLSA + "]");
							HashMap<String, String> map = (HashMap<String, String>) gytempA.get(j+mm);
							for (int m = 0; m < gytiParameterArray.length; m++) {
								//System.out.println("(MM:"+mm+")(M:"+m+")[["+ tempLSA + "]");
								
								//查找字符串是否存在，如果存在，替换，不存在，跳过
								//
								String[] tempkeywordMain = null;
								GySplit bm = new GySplit();
								bm.tool(tempLSA, gytFrontSeparator+ forKeyWord+"["+mm+"]" + "."+ gytiParameterArray[m]+ gytBackSeparator);
								tempkeywordMain = bm.Show();
								bm = null;
								//
								if(tempkeywordMain.length>=2)
								{

									GyReplace r = new GyReplace();
									r.replaceAllNew(tempLSA, gytFrontSeparator+ forKeyWord+"["+mm+"]" + "."+ gytiParameterArray[m]+ gytBackSeparator,map.get(gytiParameterArray[m]));
									tempLSS = r.Show();
									tempLSA = r.Show();
									r = null;
								}
								else
								{
									
								}
							}
						}
						
					}
					tempSS = tempSS + tempLSS;
				}
				this.sContent =  tempSS ;
				//System.out.println("[["+ tempSS + "]");
			}
		}
		
	}

	public String getsContent() {
		
		return this.sContent;
	}

	public void setcurrnS(String currnS) {
		//System.out.println("[["+ currnS + "]");
		this.currnS = currnS;
	}
	
}
